#include "http.hpp"

std::string RequestStr(const HttpRequest &req)
{
    std::string ret;
    ret += req._method + " " + req._path + " " + req._version + "\r\n";
    for (auto &it : req._params)
    {
        ret += it.first + ": " + it.second + "\r\n";
    }
    for (auto &it : req._headers)
    {
        ret += it.first + ": " + it.second + "\r\n";
    }

    ret += "\r\n";
    ret += req._body;
    return ret;
}

void Kanade(const HttpRequest &req, HttpResponse *rsp)
{
    rsp->SetContent(RequestStr(req), "text/plain");
}

void Mafuyu(const HttpRequest &req, HttpResponse *rsp)
{
    rsp->SetContent(RequestStr(req), "text/plain");
}

void Ena(const HttpRequest &req, HttpResponse *rsp)
{
    std::string filename = "./wwwroot" + req._path;
    Util::WriteFile(filename, req._body);
    // rsp->SetContent(RequestStr(req), "text/plain");
}

void Mizuki(const HttpRequest &req, HttpResponse *rsp)
{
    rsp->SetContent(RequestStr(req), "text/plain");
}

int main()
{
    HttpServer server(12700);
    server.SetThreadCount(3);
    server.SetBaseDir("./wwwroot");
    server.Get("/kanade", Kanade);
    server.Post("/mafuyu", Mafuyu);
    server.Put("/ena", Ena);
    server.Delete("/mizuki", Mizuki);
    server.Listen();

    return 0;
}